package test;

import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.function.CustomScoreProvider;

public class RecCustomScoreProvider extends CustomScoreProvider {
	
	private float ALPHA;
	private float cenx;
	private float ceny;
	private float lenx;
	private float leny;
	private IndexReader reader;
	
	public RecCustomScoreProvider(IndexReader reader, float ALPHA, 	float cenx, float ceny, float lenx, float leny) {
		super(reader);
		// TODO Auto-generated constructor stub
		
		this.ALPHA = ALPHA;
		this.cenx = cenx;
		this.ceny = ceny;
		this.lenx = lenx;
		this.leny = leny;
		this.reader = reader;
	}

	@Override
	public float customScore(int doc, float subQueryScore, float valSrcScore)
			throws IOException {
		
		float docx = Float.parseFloat(reader.document(doc).get("x"));
		float docy = Float.parseFloat(reader.document(doc).get("y"));
		float dist = (float) Math.sqrt(Math.pow(cenx - docx, 2) + Math.pow(ceny - docy, 2));
		float dismax = (float) Math.sqrt(Math.pow(lenx, 2) + Math.pow(leny, 2)) / 2;
		float sp = 1 - dist / dismax;
		float score = ALPHA * subQueryScore + (1 - ALPHA) * sp;
		return score;
	}
}
